#include <stdio.h>
#include <stdlib.h>
#include "package.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>         
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <time.h>

static char __id[64]; //车牌号
static time_t t;      //拍照时间
static int skt;       //服务器套接字
static char buffer[128]; //缓冲区

void net_init(void)   //网络初始化
{
    struct sockaddr_in sin;

    skt = socket(AF_INET,SOCK_STREAM,0);
    if(skt < 0){
        perror("net_init: socket");
        exit(-1);
    }

    sin.sin_family = AF_INET;
    sin.sin_port =  htons(6666);
    sin.sin_addr.s_addr = inet_addr("192.168.40.113");

    if(connect(skt,(const struct sockaddr*)&sin,sizeof(sin)) < 0){
        perror("connect");
        exit(-1);
    }
}

void net_close(void) //关闭网络
{
    close(skt);
}

char* get_car_id(const char *img,char *car_id)  //获取车牌号
{
    if(!img || !car_id)
    {
        printf("get_car_id:img/car_id不能为空\n");
        return NULL;
    }

    struct package p;
    struct stat buf;
    char buffer[128];
    int len,fd;

    p.protocol = 0;

    if(stat(img, &buf) < 0){
        perror("stat");
        return NULL;
    }

    t = time(NULL);
    sprintf(p.field1,"%ld",buf.st_size);
    sprintf(p.field2,"%ld",t);
    strcpy(p.field3,"0");

    const char* str = package(&p);

    if((fd = open(img,O_RDWR)) < 0){
        perror("open");
        return NULL;
    }

    send_package(skt,str);
    rcv_package(skt,buffer,128);

    while((len = read(fd,buffer,128)) > 0){
        write(skt,buffer,len);
    }

    rcv_package(skt,buffer,128);
    unpackage(buffer,&p);

    if(car_id)
        strcpy(car_id,p.field1);
    strcpy(__id,p.field1);
    return car_id;
}


int in_parking(void) //入库
{
    struct package p;
    p.protocol = 1;
    strcpy(p.field1,__id);
    sprintf(p.field2,"%ld",t);
    strcpy(p.field3,"0");

    send_package(skt,package(&p));

    while(rcv_package(skt,buffer,128) < 0);
    unpackage(buffer,&p);
    if(strncmp(p.field3,"ok",2))
        return -1;
    
    return 0;
}

int out_parking(double *min)//出库
{
    struct package p;
    p.protocol = 2;
    strcpy(p.field1,__id);
    sprintf(p.field2,"%ld",t);
    strcpy(p.field3,"0");

    send_package(skt,package(&p));

    while(rcv_package(skt,buffer,128) < 0);
    unpackage(buffer,&p);
    if(strncmp(p.field3,"ok",2))
        return -1;
    *min = atof(p.field2);
    
    return atoi(&p.field3[2]);
}


int is_parking(void)//判断是否入库
{
    struct package p;
    p.protocol = 3;
    strcpy(p.field1,__id);
    strcpy(p.field2,"0");
    strcpy(p.field3,"0");

    send_package(skt,package(&p));

    while(rcv_package(skt,buffer,128) < 0);
    unpackage(buffer,&p);
    if(!strncmp(p.field3,"ok",2))
        return 0;
    else if(!strncmp(p.field3,"parking",7))
        return 1;
    
    return -1;
}

//获取时间
time_t get_time(void)
{
    return t;
}

